/**
 * Copyright 2018 jianggujin (www.jianggujin.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jianggujin.logging;

import java.lang.reflect.Constructor;

import com.jianggujin.logging.commons.JJakartaCommonsLoggingImpl;
import com.jianggujin.logging.jdk14.JJdk14LoggingImpl;
import com.jianggujin.logging.log4j.JLog4jImpl;
import com.jianggujin.logging.nologging.JNoLoggingImpl;
import com.jianggujin.logging.slf4j.JSlf4jImpl;
import com.jianggujin.logging.stdout.JStdOutImpl;

/**
 * 日志工厂
 * 
 * @author jianggujin
 *
 */
public final class JLogFactory {

   public static final String MARKER = "JLogging";

   private static Constructor<? extends JLog> logConstructor;

   static {
      tryImplementation(new Runnable() {
         public void run() {
            useSlf4jLogging();
         }
      });
      tryImplementation(new Runnable() {
         public void run() {
            useCommonsLogging();
         }
      });
      tryImplementation(new Runnable() {
         public void run() {
            useLog4JLogging();
         }
      });
      tryImplementation(new Runnable() {
         public void run() {
            useJdkLogging();
         }
      });
      tryImplementation(new Runnable() {
         public void run() {
            useStdOutLogging();
         }
      });
      tryImplementation(new Runnable() {
         public void run() {
            useNoLogging();
         }
      });
   }

   private JLogFactory() {
   }

   public static JLog getLog(Class<?> aClass) {
      return getLog(aClass.getName());
   }

   public static JLog getLog(String logger) {
      try {
         return logConstructor.newInstance(logger);
      } catch (Throwable t) {
         throw new JLogException("Error creating logger for logger " + logger + ".  Cause: " + t, t);
      }
   }

   public static synchronized void useCustomLogging(Class<? extends JLog> clazz) {
      setImplementation(clazz);
   }

   public static synchronized void useSlf4jLogging() {
      setImplementation(JSlf4jImpl.class);
   }

   public static synchronized void useCommonsLogging() {
      setImplementation(JJakartaCommonsLoggingImpl.class);
   }

   public static synchronized void useLog4JLogging() {
      setImplementation(JLog4jImpl.class);
   }

   public static synchronized void useJdkLogging() {
      setImplementation(JJdk14LoggingImpl.class);
   }

   public static synchronized void useStdOutLogging() {
      setImplementation(JStdOutImpl.class);
   }

   public static synchronized void useNoLogging() {
      setImplementation(JNoLoggingImpl.class);
   }

   private static void tryImplementation(Runnable runnable) {
      if (logConstructor == null) {
         try {
            runnable.run();
         } catch (Throwable t) {
            // ignore
         }
      }
   }

   private static void setImplementation(Class<? extends JLog> implClass) {
      try {
         Constructor<? extends JLog> candidate = implClass.getConstructor(String.class);
         JLog log = candidate.newInstance(JLogFactory.class.getName());
         if (log.isDebugEnabled()) {
            log.debug("Logging initialized using '" + implClass + "' adapter.");
         }
         logConstructor = candidate;
      } catch (Throwable t) {
         throw new JLogException("Error setting Log implementation.  Cause: " + t, t);
      }
   }

}